﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Animation;
using Windows.UI.Xaml.Navigation;
using douban.app.Controls;
using douban.app.Extensions;
using douban.Core.Apis;
using douban.Core.ViewModels;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace douban.app.Views
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class TestPage : Page
    {
        public ObservableCollection<Group> Aitems { get; set; } 

        public TestPage()
        {
            this.InitializeComponent();

            Aitems = new ObservableCollection<Group>();

            for (int i = 0; i < 5; i++)
            {
                Aitems.Add(new Group()
                {
                    LargeAvatar = new Uri(BaseUri, "/Assets/test.jpg").AbsoluteUri,
                });
                
            }

            DataContext = this;
        }

        //private int Index
        //{
        //    get { return (int)ScrollViewer.HorizontalOffset/500; }
        //}

        //private int NextIndex { get; set; }

        //private void Previous_OnClick(object sender, RoutedEventArgs e)
        //{
        //    ScrollViewer.ScrollToHorizontalOffset(ScrollViewer.HorizontalOffset - 100);
        //}

        //private void Next_OnClick(object sender, RoutedEventArgs e)
        //{
        //    ScrollViewer.ScrollToHorizontalOffset(ScrollViewer.HorizontalOffset+100);
        //}

        //private void ScrollViewer_OnViewChanged(object sender, ScrollViewerViewChangedEventArgs e)
        //{
        //    if(ScrollViewer.HorizontalOffset.Equals(NextIndex * 500) )return;
        //    ScrollViewer.ViewChanging -= ScrollViewer_OnViewChanging;

        //    ScrollViewer.ScrollToHorizontalOffset(NextIndex * 500);
        //    ScrollViewer.ViewChanging += ScrollViewer_OnViewChanging;
        //}


        //private void ScrollViewer_OnViewChanging(object sender, ScrollViewerViewChangingEventArgs e)
        //{
        //    var finalOffset = e.FinalView.HorizontalOffset;
        //    var offset = ScrollViewer.HorizontalOffset;

        //    if (finalOffset > offset)
        //    {
        //        NextIndex = Index + 1;
        //    }
        //    else
        //    {
        //        NextIndex = Index - 1;
        //    }
        //}

        //private void ChangeItemAnimation()
        //{
        //    var storyboard = new Storyboard();
        //    var animation = new DoubleAnimation()
        //    {
        //        From = 0,
        //        To = 500,
        //        Duration = new Duration(new TimeSpan(0,0,0,1,0))
        //    };
        //    animation.EnableDependentAnimation = true;


        //    Storyboard.SetTarget(animation, ScrollViewer);
        //    Storyboard.SetTargetProperty(animation, "HorizontalOffsetProperty");
        //    storyboard.Children.Add(animation);
        //    storyboard.Begin();
        //}

        private void Click_OnClick(object sender, RoutedEventArgs e)
        {
            //ChangeItemAnimation();
        }
    }

    //public class ScrollViewerUtilities
    //{
    //    public static double GetHorizontalOffset(DependencyObject obj)
    //    {
    //        return (double)obj.GetValue(HorizontalOffsetProperty);
    //    }

    //    public static void SetHorizontalOffset(DependencyObject obj, double value)
    //    {
    //        obj.SetValue(HorizontalOffsetProperty, value);
    //    }

    //    // Using a DependencyProperty as the backing store for HorizontalOffset.  This enables animation, styling, binding, etc...
    //    public static readonly DependencyProperty HorizontalOffsetProperty =
    //        DependencyProperty.RegisterAttached("HorizontalOffset", typeof(double), 
    //            typeof(ScrollViewerUtilities), new PropertyMetadata(0, OnHorizontalOffsetChanged));



    //    private static void OnHorizontalOffsetChanged(DependencyObject d,DependencyPropertyChangedEventArgs e)
    //    {
    //        var viewer = (ScrollViewer) d;
    //        viewer.ScrollToHorizontalOffset((double)e.NewValue);
    //    }
    //}
}
